#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <cstring>

//user define
#include "axil_cmd.h"
#include "sensor_config.h"
#include "sensor_spi.h"
#include "sensor_user.h"

void sensor_config_init(){
    for(unsigned int i=0;i<370;i++){
        sensor_spi_write(i,sensor_config[i]);
    }
}

void sensor_streaming_mode(unsigned int streaming_mode_param){
    unsigned int status;
    status=sensor_spi_read(0);
    if(streaming_mode_param==0){
        sensor_spi_write(0,status&0xfe);
    }
    else{
        sensor_spi_write(0,status|0x01);
    }
}

void sensor_exp_mode(unsigned int exp_param){
    unsigned int status;
    status=sensor_spi_read(0);
    if(exp_param==0){
        sensor_spi_write(0,status&0xdf);
    }
    else{
        sensor_spi_write(0,status|0x20);
    }
}

void sensor_test_mode(unsigned int test_param){
    unsigned int status;
    status=sensor_spi_read(1);
    if(test_param==0){
        sensor_spi_write(1,status&0xfe);
    }
    else{
        sensor_spi_write(1,status|0x01);
    }
}

void sensor_flip(unsigned int flip_param){
    unsigned int status;
    status=sensor_spi_read(1);
    if(flip_param==0){
        sensor_spi_write(1,status&0xef);
    }
    else{
        sensor_spi_write(1,status|0x10);
    }
}


void sensor_exp(unsigned int exp_param[4]){
    for(unsigned int i=0;i<4;i++){
        sensor_spi_write(i+2,exp_param[i]);
    }
}

void sensor_subsmp(unsigned int subsmp_param){
    sensor_spi_write(18,subsmp_param);
}

void sensor_win1(unsigned int win1_param[4]){
    for(unsigned int i=0;i<4;i++){
        sensor_spi_write(i+19,win1_param[i]);
    }
}

void sensor_win2(unsigned int win2_param[4]){
    for(unsigned int i=0;i<4;i++){
        sensor_spi_write(i+23,win2_param[i]);
    }
}

void sensor_win3(unsigned int win3_param[4]){
    for(unsigned int i=0;i<4;i++){
        sensor_spi_write(i+27,win3_param[i]);
    }
}

void sensor_win4(unsigned int win4_param[4]){
    for(unsigned int i=0;i<4;i++){
        sensor_spi_write(i+31,win4_param[i]);
    }
}

void sensor_line_u_length(unsigned int line_u_length_param[2]){
    for(unsigned int i=0;i<2;i++){
        sensor_spi_write(i+91,line_u_length_param[i]);
    }
}

void sensor_again(unsigned int sensor_again_param){
    unsigned int status;
    status=sensor_spi_read(103);
    sensor_spi_write(103,((status&0xe0)|((sensor_again_param+11)&0x1f)));
}

void sensor_black_level(unsigned int black_level_param[2]){
    for(unsigned int i=0;i<2;i++){
        sensor_spi_write(i+322,black_level_param[i]);
    }
}

void sensor_dgain(unsigned int sensor_dgain_param){
    sensor_spi_write(349,(sensor_dgain_param+37)*4);
}

unsigned int sensor_status(unsigned int sensor_status_addr){
    return sensor_spi_read(sensor_status_addr);
}



